/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Recycle Bin
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["recyclebin"] = new Object();
myPlugins["recyclebin"]["name"] = getLocalizedString2("mod_sysm_plugin_recyclebin_name");
myPlugins["recyclebin"]["onlyonce"] = true;
myPlugins["recyclebin"]["newObject"] = function (NewSettings) { return new plugin_recyclebin_sensor(NewSettings); }

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_recyclebin_sensor(NewSettings) {
	// Required variables
	this.plugin = "recyclebin";
	this.settings = NewSettings;
	this.edit = plugin_edit;
	this.ReDraw = plugin_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#0000FF";
	if(!this.settings.warningcolor) this.settings.warningcolor = "#FF0000";
	if(!this.settings.updspeed) this.settings.updspeed = 5;
	if(!this.settings.limit) this.settings.limit = 100;
	this.settings.limit = parseInt(this.settings.limit);
	if(this.settings.limit<=0) this.settings.limit = 1;


	//The Sonsore
	this.bar = new class_PercentBar(this.frame,0,0,getLocalizedString2("mod_sysm_plugin_recyclebin_label"));
	this.bar.PercentColor(this.settings.color);
	this.bar.PercentBarColor(this.settings.barcolor);

	this.frame.onMultiClick = function ( ) { filesystem.emptyRecycleBin(); };
	this.frame.tooltip = getLocalizedString2("mod_sysm_plugin_recyclebin_tooltip");

	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_update.call(obj); };
	plugin_update.call(obj);


	// Calculating sensore height (required!)
	this.frame.height = this.bar.background.vOffset + this.bar.background.height;
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_recyclebin_pref_alertcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.warningcolor;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_recyclebin_pref_limit_label");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.limit;
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_recyclebin_pref_limit_desc");

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(3,5,10,15,30,60);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;


		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.bar.PercentColor(this.settings.color);

			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.settings.warningcolor = Drive_Edit_DialogResults[2];

			this.settings.limit = Drive_Edit_DialogResults[3];
			this.settings.limit = parseInt(this.settings.limit);
			if(this.settings.limit<=0) this.settings.limit = 1;

			if ( this.settings.updspeed != Drive_Edit_DialogResults[4] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[4];
				this.timer.interval = this.settings.updspeed;
			}
			plugin_update.call(this);
		}
		delete Drive_Edit_DialogFields;
		return true;
	}


	function plugin_update() {
		myTrash = filesystem.getRecycleBinInfo();
		this.bar.PercentBarValue( (myTrash.size/1048576) * 100 / this.settings.limit , myTrash.numItems + " #")
		if( (myTrash.size/1048576) * 100 / this.settings.limit > 100) {
			this.bar.PercentBarColor(this.settings.warningcolor);
		} else {
			this.bar.PercentBarColor(this.settings.barcolor);
		}
		return true;
	}


	function plugin_ReDraw() {
		this.frame.width = mod_sysmonitor_width;
		this.bar.style = preferences.mod_sysmonitor_bar_style.value;
		this.bar.size = mod_sysmonitor_size;
		this.frame.height = this.bar.background.vOffset + this.bar.background.height;
		return true;
	}


}

// --------------------------------------------------------------------------------
// The END